package com.metagurukul.metagranth.service;

import java.sql.SQLException;

import junit.framework.TestCase;

import com.metagurukul.metagranth.vo.User;

/*
 * this class is a jUnit class testing UserService class
 */

public class UserJUnitTEST extends TestCase {

	/**
	 * this function test the UserService class about positive case of
	 * saveUserInformation function(the user is created and saved in the
	 * database
	 * 
	 * secnario:database is having some users already and we add 1 dummy user
	 * here.all the fields are given values except last login date and type
	 * 
	 * expected result:function should add the details to database. actual
	 * result :we retrieve the status(row inserted)and should be 1.
	 * 
	 * @author rohit
	 * 
	 */
	
	public void testSaveUserInformationCheckRowInserted() {
		// test data
		String name = "test";
		String userName = "test@metagurukul.com";
		String password = "testpass";
		int age = 25;
		try {
			// lastlogindate and type is not defined but automatically
			// inserted into database.
			// create a test user.
			User testUser = new User();
			// set properties
			testUser.setName(name);
			testUser.setUsername(userName);
			testUser.setPassword(password);
			testUser.setAge(age);
			// run service method
			int rowInserted = UserService.saveUserInformation(testUser);
			assertEquals(rowInserted, 1);// status 1 means user row inserted is
			// 1.
		} catch (Exception e) {
			fail();
			e.printStackTrace();
		} finally {
			try {
				CommonJUnit.deleteTestUser();
			} catch (SQLException e) {
				fail();
				e.printStackTrace();
			}
		}
	}

	/**
	 * this function test the UserService class about positive case of
	 * saveUserInformation function(the user is created and saved in the
	 * database
	 * 
	 * secnario:database is having some users already and we add 1 test user
	 * here.all the fields are given values except last login date and type
	 * 
	 * expected result:function should add the details to database. actual
	 * result :we retrieve user by another method and check all the data is
	 * perfectly matching.
	 * 
	 * @author rohit
	 * 
	 */

	public void testSaveUserInformationCheckDataInserted() {

		// test data
		String name = "test";
		String userName = "test@metagurukul.com";
		String password = "testpass";
		int age = 25;
		try {
			// lastlogindate and type is not defined but automatically
			// inserted into database.
			// create a test user.
			User testUser = new User();
			// set properties
			testUser.setName(name);
			testUser.setUsername(userName);
			testUser.setPassword(password);
			testUser.setAge(age);
			// run service method
			int rowInserted = UserService.saveUserInformation(testUser);
			// make a new user and retrieve the info of user by a pre tested
			// method.
			User testUserRetreived = CommonJUnit.selectTestUser();
			// find if both the users are same.
			// checking name
			assertEquals(testUser.getName(), testUserRetreived.getName());
			// checking user name
			assertEquals(testUser.getUsername(),
					testUserRetreived.getUsername());
			// checking password
			assertEquals(testUser.getPassword(),
					testUserRetreived.getPassword());
			// checking age
			assertEquals(testUser.getAge(), testUserRetreived.getAge());
			// checking date(should be current date)

			// Date curDate=new Date();
			// assertEquals(curDate,testUserRetreived.getLastlogindate());
			// checking type(should be 0 as a user)
			final int SIMPLEUSER = 0;
			assertEquals(SIMPLEUSER, testUserRetreived.getType());
		} catch (Exception e) {
			fail();
			e.printStackTrace();
		} finally {
			try {
				CommonJUnit.deleteTestUser();
			} catch (SQLException e) {
				fail();
				e.printStackTrace();
			}

		}

	}

	/**
	 * this function test the UserService class about duplicacy check case of
	 * saveUserInformation function(the user is created and saved in the
	 * database
	 * 
	 * secnario:database is having some users already and we add 1 test user
	 * here.all the fields are given values except last login date and type. one
	 * test user is then again inserted with same name.
	 * 
	 * expected result:function should give error msg about duplicacy. actual
	 * result :the function will return -1 if user is already existed of same
	 * username.
	 * 
	 * @author rohit
	 * 
	 */

	public void testSaveUserInformationDuplicacyCheck() {
		// dummy data
		String name = "test";
		String userName = "test@metagurukul.com";
		String password = "testpass";
		int age = 25;
		try {
			// lastlogindate and type is not defined but automatically
			// inserted into database.
			// create a test user.
			User testUser = new User();
			// set properties
			testUser.setName(name);
			testUser.setUsername(userName);
			testUser.setPassword(password);
			testUser.setAge(age);
			// run service method
			UserService.saveUserInformation(testUser);

			// make a new user and save it again in database.
			User user = new User();
			user.setUsername(userName);
			user.setPassword(password);
			// run service method
			int status = UserService.saveUserInformation(user);
			// check for duplicacy (return status = -1)
			assertEquals(status, -1);
		} catch (Exception e) {
			fail();
			e.printStackTrace();
		} finally {
			try {
				CommonJUnit.deleteTestUser();
			} catch (SQLException e) {
				fail();
				e.printStackTrace();
			}
		}

	}

	/**
	 * this function test the UserService class about null check case of
	 * saveUserInformation function(the user is null ie not having username and
	 * saved in the database)
	 * 
	 * secnario:database is having some users already and we add 1 test user
	 * here which is not having username .
	 * 
	 * expected result:function should give error msg about null username.
	 * actual result :the function will return -1 if user is already existed of
	 * same username.
	 * 
	 * @author rohit
	 * 
	 */

	public void testSaveUserInformationUserNameNull() {
		// dummy data
		String name = "test";
		String userName = "";
		String password = "testpass";
		int age = 25;
		try {
			// lastlogindate and type is not defined but automatically
			// inserted into database.
			// create a test user.
			User testUser = new User();
			// set properties
			testUser.setName(name);
			testUser.setUsername(userName);
			testUser.setPassword(password);
			testUser.setAge(age);
			// run service method
			int status = UserService.saveUserInformation(testUser);
			assertEquals(status, 0);
		} catch (Exception e) {
			fail();
			e.printStackTrace();
		} finally {
			try {
				CommonJUnit.deleteTestUser();
			} catch (SQLException e) {
				fail();
				e.printStackTrace();
			}
		}

	}

	/**
	 * this function test the UserService class about null check case of
	 * saveUserInformation function(the user is null ie not having username and
	 * saved in the database)
	 * 
	 * secnario:database is having some users already and we add 1 test user
	 * here which is not having username .
	 * 
	 * expected result:function should give error msg about null username.
	 * actual result :the function will return -1 if user is already existed of
	 * same username.
	 * 
	 * @author rohit
	 * 
	 */

	public void testSaveUserInformationUserNull() {
		// dummy data
		try {
			// lastlogindate and type is not defined but automatically
			// inserted into database.
			// create a test user.
			User testUser = null;
			// run service method
			int status = UserService.saveUserInformation(testUser);
			assertEquals(status, -2);
		} catch (Exception e) {
			fail();
			e.printStackTrace();
		} finally {
			try {
				CommonJUnit.deleteTestUser();
			} catch (SQLException e) {
				fail();
				e.printStackTrace();
			}
		}

	}

	/**
	 * this function test the UserService class about null check of authenticate
	 * function(the user given to this function is null)
	 * 
	 * secnario:database is having some users already and give authenticate
	 * method a argument which is null user method should return null user.
	 * 
	 * expected result:function should give null user after null check. actual
	 * result :the function will return null if user is null.
	 * 
	 * @author rohit
	 * 
	 */

	public void testAuthenticateUserNull() {
		// dummy data
		try {
			// lastlogindate and type is not defined but automatically
			// inserted into database.
			// create a test user.
			User testUser = null;
			// run service method
			User status = UserService.authentication(testUser);
			assertEquals(status, null);
		} catch (Exception e) {
			fail();
			e.printStackTrace();
		} finally {
			try {
				CommonJUnit.deleteTestUser();
			} catch (SQLException e) {
				fail();
				e.printStackTrace();
			}
		}

	}

	/**
	 * this function test the UserService class about null check of authenticate
	 * function(the user name or password given to this function is null)
	 * 
	 * secnario:database is having some users already and give authenticate
	 * method a argument which have username or password null.method should
	 * return null user.
	 * 
	 * expected result:function should give null user after null check. actual
	 * result :the function will return null if user is null.
	 * 
	 * @author rohit
	 * 
	 */

	public void testAuthenticateUserNameOrPasswordNull() {
		// dummy data
		try {
			// lastlogindate and type is not defined but automatically
			// inserted into database.
			// create a test user.
			User testUser = new User();
			testUser.setUsername("");
			testUser.setPassword("");
			// run service method
			User status = UserService.authentication(testUser);
			assertEquals(status, null);
		} catch (Exception e) {
			fail();
			e.printStackTrace();
		} finally {
			try {
				CommonJUnit.deleteTestUser();
			} catch (SQLException e) {
				fail();
				e.printStackTrace();
			}
		}

	}

	/**
	 * this function test the UserService class about wrong password but right
	 * user name in authenticate function (the user name is right but password
	 * given to this function is wrong)
	 * 
	 * secnario:database is having some users already and we add a test user
	 * Then give authenticate method a user which have username right but
	 * password wrong.method should return null user.
	 * 
	 * expected result:function should give null user after checking. actual
	 * result :the function will return null.
	 * 
	 * @author rohit
	 * 
	 */

	public void testAuthenticateUserNameRightPasswordWrong() {
		// dummy data
		try {
			// insert a test user in database.
			CommonJUnit.createTestUser();
			// create a test user.
			User testUser = new User();
			testUser.setUsername("test@metagurukul.com");// username is right
			testUser.setPassword("asdf");// password is wrong
			// run service method
			User status = UserService.authentication(testUser);
			assertEquals(status, null);
		} catch (Exception e) {
			fail();
			e.printStackTrace();
		} finally {
			try {
				CommonJUnit.deleteTestUser();
			} catch (SQLException e) {
				fail();
				e.printStackTrace();
			}
		}

	}

	/**
	 * this function test the UserService class about wrong password but right
	 * user name in authenticate function (the user name is right but password
	 * given to this function is wrong)
	 * 
	 * secnario:database is having some users already and we add a test user
	 * Then give authenticate method a user which have username right but
	 * password wrong.method should return null user.
	 * 
	 * expected result:function should give null user after checking. actual
	 * result :the function will return null.
	 * 
	 * @author rohit
	 * 
	 */

	public void testAuthenticateUserNameWrongPasswordRight() {
		// dummy data
		try {
			// inser a test user in database.
			CommonJUnit.createTestUser();
			// create a test user.
			User testUser = new User();
			testUser.setUsername("teasdf@metagurukul.com");// username is wrong
			testUser.setPassword("testpass");// password is right
			// run service method
			User status = UserService.authentication(testUser);
			assertEquals(status, null);
		} catch (Exception e) {
			fail();
			e.printStackTrace();
		} finally {
			try {
				CommonJUnit.deleteTestUser();
			} catch (SQLException e) {
				fail();
				e.printStackTrace();
			}
		}

	}

	/**
	 * this function test the UserService class about right password and right
	 * user name in authenticate function (the user name is right and password
	 * is also right )
	 * 
	 * secnario:database is having some users already and we add a test user
	 * Then give authenticate method a user which have username right and
	 * password right.method should return test user.
	 * 
	 * expected result:function should return test user after checking. actual
	 * result :the function will return test user.
	 * 
	 * @author rohit
	 * 
	 */

	public void testAuthenticateUserNameRightPasswordRight() {
		try {
			// inser a test user in database.
			CommonJUnit.createTestUser();
			// create a test user.
			User testUser = new User();
			testUser.setUsername("test@metagurukul.com");// username is right
			testUser.setPassword("testpass");// password is right
			// run service method
			User testUserRetreived = UserService.authentication(testUser);
			// checking name
			assertEquals("test", testUserRetreived.getName());
			// checking user name
			assertEquals(testUser.getUsername(),
					testUserRetreived.getUsername());
			// checking password
			assertEquals(testUser.getPassword(),
					testUserRetreived.getPassword());
			// checking age
			assertEquals(25, testUserRetreived.getAge());
			// checking date(should be current date)

			// Date curDate=new Date();
			// assertEquals(curDate,testUserRetreived.getLastlogindate());
			// checking type(should be 0 as a user)
			final int SIMPLEUSER = 0;
			assertEquals(SIMPLEUSER, testUserRetreived.getType());
		} catch (Exception e) {
			fail();
			e.printStackTrace();
		} finally {
			try {
				CommonJUnit.deleteTestUser();
			} catch (SQLException e) {
				fail();
				e.printStackTrace();
			}
		}

	}

	/**
	 * this function test the UserService class about new password null
	 * of ChangePassword method.
	 * 
	 * secnario:database is having some users already and we add a test user
	 * Then change password method is called with new password null.
	 * 
	 * expected result:function should return 0 status as a error code.
	 * 
	 * @author rohit
	 * 
	 */

	public void testChangePasswordNewPasswordNull() {
		try {
			// insert a test user in database.
			int userId=CommonJUnit.createTestUser();
			// create a test user.
			User testUser = new User();
			testUser.setUid(userId);
			String newPassword=null;
			String conNewPassword=null;
		int status=	UserService.changePassword(testUser, newPassword, conNewPassword);
			assertEquals(status,0);
		} catch (Exception e) {
			fail();
			e.printStackTrace();
		} finally {
			try {
				CommonJUnit.deleteTestUser();
			} catch (SQLException e) {
				fail();
				e.printStackTrace();
			}
		}

	}
	/**
	 * this function test the UserService class about new password and confirm new password
	 * not match in changePassword method.
	 * 
	 * secnario:database is having some users already and we add a test user
	 * Then change password method is called with new password and confirm new password not matched..
	 * 
	 * expected result:function should return 1 status as a error code.
	 * 
	 * @author rohit
	 * 
	 */

	public void testChangePasswordNewPasswordConfirmNewPasswordNotMatch() {
		try {
			// insert a test user in database.
			int userId=CommonJUnit.createTestUser();
			// create a test user.
			User testUser = new User();
			testUser.setUid(userId);
			String newPassword="new password";
			String conNewPassword="confirm new password";
		int status=	UserService.changePassword(testUser, newPassword, conNewPassword);
			assertEquals(status,1);
		} catch (Exception e) {
			fail();
			e.printStackTrace();
		} finally {
			try {
				CommonJUnit.deleteTestUser();
			} catch (SQLException e) {
				fail();
				e.printStackTrace();
			}
		}

	}
	
	/**
	 * this function test the UserService class about new password and confirm new password
	 * not match in changePassword method.
	 * 
	 * secnario:database is having some users already and we add a test user
	 * Then change password method is called with new password and confirm new password not matched..
	 * 
	 * expected result:function should return 1 status as a error code.
	 * 
	 * @author rohit
	 * 
	 */

	public void testChangePasswordPositiveCAse() {
		try {
			// insert a test user in database.
			int userId=CommonJUnit.createTestUser();
			// create a test user.
			User testUser = new User();
			testUser.setUid(userId);
			String newPassword="password";
			String conNewPassword="password";
			String userName = "test@metagurukul.com";
			testUser.setUsername(userName);
		int status=	UserService.changePassword(testUser, newPassword, conNewPassword);
		User user=CommonJUnit.selectTestUser();
			assertTrue(user.getPassword().equals(newPassword));
		} catch (Exception e) {
			e.printStackTrace();
			fail();
		} finally {
			try {
				CommonJUnit.deleteTestUser();
			} catch (SQLException e) {
				fail();
				e.printStackTrace();
			}
		}

	}
	
	
}
